var fso = WScript.CreateObject('Scripting.FileSystemObject');

var version = 'Gimme v2.7.0 (Edmund) :: ' + new Date().toLocaleString();

var buildFile = 'gimme.build';
var dependencyFile = 'gimme.dependencies';
var outputFile = '.\\build\\debug\\gimme.js';
var srcPath = '.\\src\\';
var altSyntaxPattern = /(\n\s*)\/\/\[AltSyntax\]/g;

var useAltSyntax = false;

var args = WScript.Arguments;
var i, len = args.length;
if (len % 2 === 0)
{
	for (i = 0; i < len; i += 2)
	{
		switch (args.Item(0).toLowerCase())
		{
			case '/altsyntax':
				useAltSyntax = args.Item(i+1).toLowerCase() === 'true' ? true : false;
				break;
		}
	}
	
	go();
}
else
{
	WScript.Echo('Invalid number of command line arguments specified.');	
}


function generateBuildList(dependencies)
{
	var buildList = fso.OpenTextFile(buildFile).ReadAll().split('\r\n');
	buildList.contains = function(item)
	{
		var i = this.length;
		while (i--)
		{
			if (this[i] === item)
			{
				return true;
			}
		}
		return false;
	};
	
	var helperIndex = -1;
	var fetchFound = false;
	
	var i = 0
	while (i < buildList.length)
	{
		var code = buildList[i];	
		if (!code)
		{
			buildList.splice(i, 1);
			continue;
		}
		else
		{
			if (code.charAt(0) === '#')
			{
				buildList.splice(i, 1);
				code = code.substring(1);
			}
			
			if (code === 'fetch')
			{
				fetchFound = true;
			}
			
			if (code === 'gimme.helper')
			{
				helperIndex = i;
			}
			
			i++;
		}
		
		if (dependencies[code])
		{
			WScript.Echo(code + ' has dependencies on: ');
			var j, len = dependencies[code].length;
			for (j = 0; j < len; j++)
			{
				WScript.Echo('\t' + dependencies[code][j]);
				if (!buildList.contains(dependencies[code][j]))
				{
					buildList.push(dependencies[code][j]);
				}
			}
			WScript.Echo('');
		}
	}
	
	if (helperIndex !== -1 && !fetchFound)
	{
		buildList[helperIndex] = 'gimme.helper2';
	}	
	
	return buildList;
}

function readDependencyFile()
{
	return eval('(' + fso.OpenTextFile(dependencyFile).ReadAll() + ')');
}
function readFile(file, file2)
{
	if (file2)
	{
		file = file2;
	}

	WScript.Echo(file);
	var f = WScript.CreateObject('Scripting.FileSystemObject');
	var content = f.OpenTextFile(file).ReadAll();
	content = content.replace(/#include\(([^\)]+)\)/g, readFile);
	return content;
}

function generateOutputFile(buildList)
{
	var gimmejs = fso.OpenTextFile(outputFile, 2, true);
	var i, len = buildList.length;
	for (i = 0; i < len; i++)
	{
		var content = readFile(srcPath + buildList[i] + '.js'); // fso.OpenTextFile(srcPath + buildList[i] + '.js').ReadAll();
		if (useAltSyntax)
		{
			content = content.replace(altSyntaxPattern, '$1/*AltSyntax On*/ ');
		}
		gimmejs.Write(content);
	}
	
	gimmejs.WriteLine('Gimme.toString = function() { return \'' + version + '\'; };');
	gimmejs.Close();
	fso = null;	
}

function go()
{
	var depends = readDependencyFile();
	var list = generateBuildList(depends);

	var i, len = list.length;
	WScript.Echo('Final Build List:');
	for (i = 0; i < len; i++)
	{
		WScript.Echo('\t' + list[i]);
	}
	
	generateOutputFile(list);	
}

